package cn.wizzer.app.web.modules.controllers.user;


import cn.wizzer.app.user.modules.models.Gift;
import cn.wizzer.app.user.modules.services.GiftOrderService;
import cn.wizzer.app.user.modules.services.GiftService;
import cn.wizzer.app.web.commons.ulog.annotation.ULog;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@Api(value = "/api/gift", tags = "礼物模块")
@IocBean
@At("/api/gift")
public class ApiGiftController {
    private final static Log log = Logs.get();


    @Inject
    @Reference
    private GiftService giftService;

    @Inject
    @Reference
    private GiftOrderService orderService;


    @ApiOperation(value = "礼物列表", notes = "礼物列表", httpMethod = "POST", response = Object.class)
    @At("/list")
    @Ok("json")
    public Object list(
            @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize
    ) {
        try {
            Cnd cnd = Cnd.where("disabled", "=", "0").and("delFlag", "=", "0");
            cnd.orderBy("price", "asc");
            return Result.success().addData(giftService.listPage(pageNumber, pageSize, cnd));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error();
        }
    }

    @ApiOperation(value = "礼物流水", notes = "礼物流水", httpMethod = "POST", response = Object.class)
    @At("/order/create")
    @Ok("json:full")
    @ULog(tag = "用户赠送礼物", msg = "礼物: ${giftId}, 数量: ${num}, 赠送用户: ${toUser}, 直播间: ${liveNo}")
    public Object create(
            @ApiParam(required = true, value = "giftId") @Param("giftId") String giftId,
            @ApiParam(required = true, value = "num") @Param("num") Integer num,
            @ApiParam(required = true, value = "toUser") @Param("toUser") String toUser,
            @ApiParam(required = true, value = "liveNo") @Param("liveNo") String liveNo) {
        String appid = Mvcs.getReq().getHeader("appid");
        orderService.create(giftId, num, appid, toUser, liveNo);
        Gift gift = giftService.fetch(giftId);
        NettyMsgUtil.sendGiftMessage(liveNo, appid, giftId, num, gift.getName(), gift.getCoverPic());
        return Result.success();
    }

    @ApiOperation(value = "礼物流水", notes = "礼物流水", httpMethod = "POST", response = Object.class)
    @At("/order/record")
    @Ok("json")
    public Object orderList(
            @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize,
            @Param("toMe") boolean toMe) {
        String appid = Mvcs.getReq().getHeader("appid");
        return Result.success().addData(orderService.data(pageNumber, pageSize, toMe, appid, null, null));
    }
}
